﻿//-----------------------------------------------------------------------------
//<filedescription file="IntegrationKit.cs" company="Microsoft">
//  <copyright>
//     Copyright © Microsoft Corporation.  All rights reserved.
//     ***  DESIGNATED SAMPLE CODE (per the included software license terms) ***
//      THIS CODE SAMPLE IS MADE AVAILABLE TO YOU WITHOUT WARRANTY OF ANY KIND
//      AND CONFERS NO RIGHTS ON YOU WHATSOEVER EXCEPT THE RIGHT TO REVIEW IT
//      SOLELY AS A REFERENCE.  THE ENTIRE RISK OF USE OR RESULTS FROM USE OF
//      THIS CODE SAMPLE REMAINS WITH YOU.
//  </copyright>
//  <purpose>
//     The main body of the Integration Kit methods, which are general-purpose
//     or utility methods related to Configuration Manager and the SDK.
//  </purpose>
//  <notes>
//  </notes>
//</filedescription>
//-----------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Management;
using System.Net;
using System.Net.Mail;
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using Microsoft.ConfigurationManagement.AdminConsole;
using Microsoft.ConfigurationManagement.AdminConsole.Common;
using Microsoft.ConfigurationManagement.AdminConsole.OsdCommon;
using Microsoft.ConfigurationManagement.AdminConsole.TaskSequenceEditor;
using Microsoft.ConfigurationManagement.ManagementProvider;
using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;
using Microsoft.Win32;


namespace Microsoft.ConfigurationManagement.IntegrationDevelopmentKit
{
    /// <summary>
    /// 
    /// </summary>
    public partial class IntegrationKitUtilities
    {
        
        #region Properties...

        private static StreamWriter logFile;

        /// <summary>
        /// Gets or sets file stream object created by the custom action for writing the log file.
        /// </summary>
        public static StreamWriter LogFile
        {
            get { return logFile; }
            set { logFile = value; }
        }

        private static bool debugEnabled;
        /// <summary>
        /// Gets or sets a boolean value indicating whether or not debug logging is enabled.
        /// </summary>
        public static bool DebugEnabled
        {
            get { return debugEnabled; }
            set { debugEnabled = value; }
        }

  
        #endregion
        
        #region Enums

        /// <summary>
        /// Contains values used by Configuration Manager indicating the container node type of a tree node (folder) in the console.
        /// </summary>
        public enum ContainerItemType
        {
            /// <summary>
            ///
            /// </summary>
            None = 0,
            /// <summary>
            /// 
            /// </summary>
            Package = 2,
            /// <summary>
            /// 
            /// </summary>
            Advertisement = 3,
            /// <summary>
            /// 
            /// </summary>
            Query = 7,
            /// <summary>
            /// 
            /// </summary>
            Report = 8,
            /// <summary>
            /// 
            /// </summary>
            MeteredProductRule = 9,
            /// <summary>
            /// 
            /// </summary>
            ConfigurationItem = 11,
            /// <summary>
            /// 
            /// </summary>
            OperatingSystemInstallPackage = 14,
            /// <summary>
            /// 
            /// </summary>
            StateMigration = 17,
            /// <summary>
            /// 
            /// </summary>
            ImagePackage = 18,
            /// <summary>
            /// 
            /// </summary>
            BootImagePackage = 19,
            /// <summary>
            /// 
            /// </summary>
            TaskSequencePackage = 20,
            /// <summary>
            /// 
            /// </summary>
            DeviceSettingPackage = 21,
            /// <summary>
            /// 
            /// </summary>
            DriverPackage = 23,
            /// <summary>
            /// 
            /// </summary>
            Driver = 25,
            /// <summary>
            /// 
            /// </summary>
            SoftwareUpdate = 1011,
            /// <summary>
            /// 
            /// </summary>
            ConfigurationBaseline = 2011
        }

        /// <summary>
        /// The type of the package
        /// </summary>
        public enum PackageType
        {
            /// <summary>
            /// Regular software distribution package
            /// </summary>
            SWDPackage = 0,
            /// <summary>
            /// Driver package.
            /// </summary>
            DriverPackage = 3,
            /// <summary>
            /// Task sequence package.
            /// </summary>
            TaskSequencePackage = 4,
            /// <summary>
            /// Software update package.
            /// </summary>
            SoftwareUpdatePackage = 5,
            /// <summary>
            /// Device setting package
            /// </summary>
            DeviceSettingPackage = 6,
            /// <summary>
            /// Image package.
            /// </summary>
            OSImagePackage = 257,
            /// <summary>
            /// Boot image package.
            /// </summary>
            BootImagePackage = 258,
            /// <summary>
            /// Operating system install package
            /// </summary>
            OSInstallPackage = 259
        }

        /// <summary>
        /// Flags specifying special properties of the package. 
        /// </summary>
        public enum PackageFlag
        {
            /// <summary>
            ///     
            /// </summary>
            /// <remarks>
            ///     
            /// </remarks>
            ERROR = 0,
            /// <summary>
            /// Do not download the package to branch distribution points, as it will be pre-staged.
            /// </summary>
            DO_NOT_DOWNLOAD = 24,

            /// <summary>
            /// Persist the package in the cache.
            /// </summary>
            PERSIST_IN_CACHE = 25,

            /// <summary>
            /// Marks the package to be replicated by distribution manager using binary delta replication.
            /// </summary>
            USE_BINARY_DELTA_REP = 26,

            /// <summary>
            /// The package does not require distribution points.
            /// </summary>
            NO_PACKAGE = 28,

            /// <summary>
            /// This value determines if Configuration Manager uses MIFName, MIFPublisher, and MIFVersion for MIF file status matching. Otherwise, Configuration Manager uses Name, Manufacturer, and Version for status matching. For more information, see the Remarks section later in this topic.
            /// </summary>
            USE_SPECIAL_MIF = 29,

            /// <summary>
            /// The package is allowed to be distributed on demand to branch distribution points.
            /// </summary>
            DISTRIBUTE_ON_DEMAND = 30,
        }

        /// <summary>
        /// Flag indicating the method of reading the package source files. The default value is StorageNoSource (1).
        /// </summary>
        public enum PackageSourceFlag
        {
            /// <summary>
            /// The user specifies the source file storage.
            /// </summary>
            StorageNeedsSpecifying = 0,

            /// <summary>
            /// The program does not use source files.
            /// </summary>
            StorageNoSource = 1,

            /// <summary>
            /// Take source files directly from the source without compression. Use this flag when the source files are located on the local server or when a Universal Naming Convention (UNC) path has been specified to a persistent storage location.
            /// </summary>
            StorageDirect = 2,

            /// <summary>
            /// Compress the source files and copy them to the site server. Use this flag when the package source files are coming from a CD or a nonpersistent medium. Configuration Manager puts the path to where the files were copied in StoredPkgPath. 
            /// You cannot specify a refresh schedule when you select this flag. If you call the RefreshPkgSource method, the CD or specified source medium must be in a ready state.
            /// </summary>
            StorageCompress = 3,

            /// <summary>
            /// Take source files from a local source.
            /// </summary>
            StorageLocal = 4,
        }

        /// <summary>
        /// Defines the operators used when doing comparison operations.
        /// </summary>
        public enum ConstraintOperator
        {
            /// <summary>
            /// None (Default)    
            /// </summary>
            /// <remarks>
            ///     
            /// </remarks>
            None=0,

            /// <summary>
            /// Equals
            /// </summary>
            Equals,

            /// <summary>
            /// Not equals
            /// </summary>
            NotEquals,

            /// <summary>
            /// Is equal to at least one of the specified values
            /// </summary>
            OneOf,

            /// <summary>
            /// Is greater than the specified value
            /// </summary>
            GreaterThan,

            /// <summary>
            /// Is less than the specified value
            /// </summary>
            LessThan,

            /// <summary>
            /// Is between the spcified values
            /// </summary>
            Between,

            /// <summary>
            /// Begins with the specified string value
            /// </summary>
            BeginsWith,

            /// <summary>
            /// Ends with the specified string value
            /// </summary>
            EndsWith,

            /// <summary>
            /// Contains the spcified string value
            /// </summary>
            Contains,

            /// <summary>
            /// Matches the specified RegEx expression
            /// </summary>
            Matches,

            /// <summary>
            /// All of the lsited values match
            /// </summary>
            AllOf,

            /// <summary>
            /// Does not match any of the listed values
            /// </summary>
            NoneOf,

            /// <summary>
            /// Greater than or equals
            /// </summary>
            GreaterEquals,

            /// <summary>
            /// Less than or equals
            /// </summary>
            LessEquals,

            /// <summary>
            /// Does not begin with the specified string value
            /// </summary>
            NotBeginsWith,

            /// <summary>
            /// Does not end with the specified string
            /// </summary>
            NotEndsWith,

            /// <summary>
            /// Does not contain the specified string value
            /// </summary>
            NotContains,

            /// <summary>
            /// Does not match the specified RegEx pattern
            /// </summary>
            NotMatches,

            /// <summary>
            /// Value is null
            /// </summary>
            IsNull,

            /// <summary>
            /// 
            /// </summary>
            AtLeast,

            /// <summary>
            /// 
            /// </summary>
            Exactly,

            /// <summary>
            /// Unspecified operator, perhpas externally defined
            /// </summary>
            Other
        }

        /// <summary>
        /// Defines values associated with registry lookups.
        /// </summary>
        public enum RegistryType
        {
            /// <summary>
            /// 
            /// </summary>
            NONE,
            /// <summary>
            /// 
            /// </summary>
            REG_BINARY,
            /// <summary>
            /// 
            /// </summary>
            REG_DWORD,
            /// <summary>
            /// 
            /// </summary>
            REG_EXPAND_SZ,
            /// <summary>
            /// 
            /// </summary>
            REG_MULTI_SZ,
            /// <summary>
            /// 
            /// </summary>
            REG_NONE,
            /// <summary>
            /// 
            /// </summary>
            REG_QWORD,
            /// <summary>
            /// 
            /// </summary>
            REG_SZ
        }

        /// <summary>
        /// Flags identifying the installation characteristics of the program. Possible values are listed below. 
        /// The default values are EVERYUSER, USEUNCPATH, USERCONTEXT, and UNATTENDED. 
        /// Ensure that no conflicting values are selected. For example, NOUSERLOGGEDIN and USERCONTEXT should not be used together.
        /// </summary>
        public enum ProgramFlag
        {
            /// <summary>
            /// The program is authorized for dynamic install.
            /// </summary>
            AUTHORIZED_DYNAMIC_INSTALL = 0,

            /// <summary>
            /// The task sequence shows a custom progress user interface message
            /// </summary>
            USECUSTOMPROGRESSMSG = 1,

            /// <summary>
            /// This is a default program
            /// </summary>
            DEFAULT_PROGRAM = 4,

            /// <summary>
            /// Disables MOM alerts while the program runs.
            /// </summary>
            DISABLEMOMALERTONRUNNING = 5,

            /// <summary>
            /// Generates MOM alert if the program fails
            /// </summary>
            MOMALERTONFAIL = 6,

            /// <summary>
            /// If set, this program's immediate dependent should always be run
            /// </summary>
            RUN_DEPENDANT_ALWAYS = 7,

            /// <summary>
            /// Indicates a device program. If set, the program is not offered to desktop clients
            /// </summary>
            WINDOWS_CE = 8,

            /// <summary>
            /// The countdown dialog is not displayed.
            /// </summary>
            COUNTDOWN = 10,

            /// <summary>
            /// The program is disabled.
            /// </summary>
            /// 
            DISABLED = 12,
            /// <summary>
            /// The program requires no user interaction.
            /// </summary>
            UNATTENDED = 13,

            /// <summary>
            /// The program can run only when a user is logged on.
            /// </summary>
            USERCONTEXT = 14,

            /// <summary>
            /// The program must be run as the local Administrator account.
            /// </summary>
            ADMINRIGHTS = 15,

            /// <summary>
            /// The program must be run by every user for whom it is valid. Valid only for mandatory jobs.
            /// </summary>
            EVERYUSER = 16,

            /// <summary>
            /// The program is run only when no user is logged on.
            /// </summary>
            NOUSERLOGGEDIN = 17,

            /// <summary>
            /// The program will restart the computer.
            /// </summary>
            OKTOQUIT = 18,

            /// <summary>
            /// Configuration Manager restarts the computer when the program has finished running successfully.
            /// </summary>
            OKTOREBOOT = 19,

            /// <summary>
            /// Use a UNC path (no drive letter) to access the distribution point.
            /// </summary>
            USEUNCPATH = 20,

            /// <summary>
            /// Persists the connection to the drive specified in the DriveLetter property. The USEUNCPATH bit flag must not be set.
            /// </summary>
            PERSISTCONNECTION = 21,

            /// <summary>
            /// Run the program as a minimized window.
            /// </summary>
            RUNMINIMIZED = 22,

            /// <summary>
            /// Run the program as a maximized window.
            /// </summary>
            RUNMAXIMIZED = 23,

            /// <summary>
            /// Hide the program window.
            /// </summary>
            HIDEWINDOW = 24,

            /// <summary>
            /// Logoff user when program completes successfully.
            /// </summary>
            OKTOLOGOFF = 25,

            /// <summary>
            /// This value is not used.
            /// </summary>
            RUNACCOUNT = 26,

            /// <summary>
            /// Override check for platform support.
            /// </summary>
            ANY_PLATFORM = 27,

            /// <summary>
            /// This value is not used.
            /// </summary>
            STILL_RUNNING = 28,

            /// <summary>
            /// Run uninstall from the registry key when the advertisement expires.
            /// </summary>
            SUPPORT_UNINSTALL = 29

        }

        /// <summary>
        /// Flags describing the device associated with the program. 
        /// </summary>
        public enum DeviceFlag
        {
            /// <summary>
            ///     
            /// </summary>
            /// <remarks>
            ///     
            /// </remarks>
            ERROR = 0,
            /// <summary>
            /// Always assign program to the client.
            /// </summary>
            AlwaysAssign = 24,

            /// <summary>
            /// Assign only if the device is currently connected to a high-bandwidth connection (default above 60 KBps).
            /// </summary>
            AssignWhenHighSpeed = 25,

            /// <summary>
            ///  Assign only if the device is docked, that is, it is attached to a desktop that is using ActiveSync.
            /// </summary>
            AssignWhenDocked = 26

        }

        /// <summary>
        /// 
        /// </summary>
        public enum SoftwareUpdateScheduleType
        {
            /// <summary>
            /// No schedule
            /// </summary>
            None = 0,

            /// <summary>
            /// Schedule reboot.
            /// </summary>
            Reboot = 1,

            /// <summary>
            /// Schedule recurrence
            /// </summary>
            Recurrence = 2,

            /// <summary>
            /// Schedule is mandatory.
            /// </summary>
            Mandatory = 3,

            /// <summary>
            /// Schedule is undefined
            /// </summary>
            UserDefined = 4

        }

        /// <summary>
        /// Flags indicating how the advertisement should be announced to the user. Possible values are listed below. The default value is 0.
        /// These flags must be coordinated with the flags that are specified in the ProgramFlags property of the advertised program. 
        /// For example, if you set ONUSERLOGOFF, the NOUSERLOGGEDIN flag in the program must be set. 
        /// If the flag settings do not match, the program is not advertised. For more information, see SMS_Program Server WMI Class.
        /// 
        /// Setting IMMEDIATE, ONUSERLOGON, or ONUSERLOGOFF or providing an AssignedSchedule value makes the advertised program mandatory. 
        /// A mandatory program is run automatically after the client has received the advertisement. The client cannot reject or postpone the installation.
        /// 
        /// Set the NO_DISPLAY and ONSLOWNET bits only when the IMMEDIATE, ONUSERLOGON, or ONUSERLOGOFF bit is set or the program has an AssignedSchedule value.
        /// 
        /// Set NO_DISPLAY when you do not want the user to run programs independently of the assignment. 
        /// If you do not set this flag, the advertisement is shown in the list of advertisements and can be run independently of the assignment. 
        /// The program can still be mandatory.
        /// 
        /// Set ONSLOWNET when assignments are mandatory over a slow network connection, for example, when a computer connects using a modem.
        /// </summary>
        public enum AdvertisementFlag
        {
            /// <summary>
            ///     
            /// </summary>
            /// <remarks>
            ///     
            /// </remarks>
            ERROR = 0,
            
            /// <summary>
            /// Announce the advertisement to the user immediately.
            /// </summary>
            IMMEDIATE = 5,

            /// <summary>
            /// Announce the advertisement to the user on system startup.
            /// </summary>
            ONSYSTEMSTARTUP = 8,

            /// <summary>
            /// Announce the advertisement to the user on logon.
            /// </summary>
            ONUSERLOGON = 9,

            /// <summary>
            /// Announce the advertisement to the user on logoff.
            /// </summary>
            ONUSERLOGOFF = 10,

            /// <summary>
            /// The advertisement is for a device client.
            /// </summary>
            WINDOWS_CE = 15,

            /// <summary>
            /// Do not fall back to unprotected distribution points.
            /// </summary>
            DONOT_FALLBACK = 17,

            /// <summary>
            /// The task sequence is available to removable media and the pre-boot execution environment (PXE) service point.
            /// </summary>
            ENABLE_TS_FROM_CD_AND_PXE = 18,

            /// <summary>
            /// Override maintenance windows in announcing the advertisement to the user.
            /// </summary>
            OVERRIDE_SERVICE_WINDOWS = 20,

            /// <summary>
            /// Reboot outside of maintenance windows.
            /// </summary>
            REBOOT_OUTSIDE_OF_SERVICE_WINDOWS = 21,

            /// <summary>
            /// Announce the advertisement to the user with Wake On LAN enabled.
            /// </summary>
            WAKE_ON_LAN_ENABLED = 22,

            /// <summary>
            /// Announce the advertisement to the user showing task sequence progress.
            /// </summary>
            SHOW_PROGRESS = 23,

            /// <summary>
            /// The user should not run programs independently of the assignment.
            /// </summary>
            NO_DISPLAY = 25,

            /// <summary>
            /// Assignments are mandatory over a slow network connection.
            /// </summary>
            ONSLOWNET = 26
        }

        /// <summary>
        /// Current status of the collection. 
        /// </summary>
        public enum CollectionStatus
        {
            /// <summary>
            /// 
            /// </summary>
            NONE = 0,
            /// <summary>
            /// 
            /// </summary>
            READY = 1,
            /// <summary>
            /// 
            /// </summary>
            REFRESHING = 2,
            /// <summary>
            /// 
            /// </summary>
            SAVING = 3,
            /// <summary>
            /// 
            /// </summary>
            EVALUATING = 4,
            /// <summary>
            /// 
            /// </summary>
            AWAITING_REFRESH = 5,
            /// <summary>
            /// 
            /// </summary>
            DELETING = 6,
            /// <summary>
            /// 
            /// </summary>
            APPENDING_MEMBER = 7,
            /// <summary>
            /// 
            /// </summary>
            QUERYING = 8
        }

        /// <summary>
        /// The current action that is being performed on the package by Configuration Manager. 
        /// </summary>
        public enum ActionInProgress
        {
            /// <summary>
            /// 
            /// </summary>
            NONE = 0,
            /// <summary>
            /// 
            /// </summary>
            UPDATE = 1,
            /// <summary>
            /// 
            /// </summary>
            ADD = 2,
            /// <summary>
            /// 
            /// </summary>
            DELETE = 3,
        }

        /// <summary>
        /// Day of the week when the event is scheduled to occur. The default value is 1.
        /// </summary>
        public enum DayOfWeek
        {
            /// <summary>
            ///     
            /// </summary>
            /// <remarks>
            ///     
            /// </remarks>
            ERROR = 0,
            /// <summary>
            /// 
            /// </summary>
            SUNDAY = 1,
            /// <summary>
            /// 
            /// </summary>
            MONDAY = 2,
            /// <summary>
            /// 
            /// </summary>
            TUESDAY = 3,
            /// <summary>
            /// 
            /// </summary>
            WEDNESDAY = 4,
            /// <summary>
            /// 
            /// </summary>
            THURSDAY = 5,
            /// <summary>
            /// 
            /// </summary>
            FRIDAY = 6,
            /// <summary>
            /// 
            /// </summary>
            SATURDAY = 7
        }

        /// <summary>
        /// Week of the month when the event is scheduled to occur. The default value is 0.
        /// </summary>
        public enum WeekOrder
        {
            /// <summary>
            /// 
            /// </summary>
            LAST = 0,
            /// <summary>
            /// 
            /// </summary>
            FIRST = 1,
            /// <summary>
            /// 
            /// </summary>
            SECOND = 2,
            /// <summary>
            /// 
            /// </summary>
            THIRD = 3,
            /// <summary>
            /// 
            /// </summary>
            FOURTH = 4
        }

        /// <summary>
        /// Current status of the site. The default value is ACTIVE (1).
        /// The values correspond to the values for the Status property in the Configuration Manager 2007 site configuration SMS_Site Server WMI Class. 
        /// </summary>
        public enum SiteStatus
        {
            /// <summary>
            ///     
            /// </summary>
            /// <remarks>
            ///     
            /// </remarks>
            ERROR = 0,
            
            /// <summary>
            /// 
            /// </summary>
            ACTIVE = 1,
            /// <summary>
            /// 
            /// </summary>
            PENDING = 2,
            /// <summary>
            /// 
            /// </summary>
            FAILED = 3,
            /// <summary>
            /// 
            /// </summary>
            DELETED = 4,
            /// <summary>
            /// 
            /// </summary>
            UPGRADE = 5,
        }

        /// <summary>
        /// Type of site. The default value is SECONDARY (1).
        /// The values correspond to the values for the Type property in the Configuration Manager 2007 site configuration SMS_Site Server WMI Class. 
        /// </summary>
        public enum SiteType
        {
            /// <summary>
            ///     
            /// </summary>
            /// <remarks>
            ///     
            /// </remarks>
            ERROR = 0,
            
            /// <summary>
            /// 
            /// </summary>
            SECONDARY = 1,
            /// <summary>
            /// 
            /// </summary>
            PRIMARY = 2
        }

        #endregion
              
    }
}